package com.baicun.wish.controller;

import cn.hutool.json.JSONObject;
import com.baicun.wish.entity.CollegeAdmit;
import com.baicun.wish.entity.ProvinceAdmit;
import com.baicun.wish.entity.dto.PredictionNextYear;
import com.baicun.wish.entity.dto.Result;
import com.baicun.wish.service.CollegeAdmitService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baicun
 * @since 2020-09-09
 */
@RestController
@RequestMapping("/college-admit")
public class CollegeAdmitController {

    @Autowired
    private CollegeAdmitService collegeAdmitService;

    /**
     * create index index_getCollegeAdmitByCollegeName on college_admit(college_name,province);
     * 根据collegeName 省份 分页查询college_admit 中 大学在各省的录取信息
     * @param jsonObject
     * @return
     */
    @PostMapping("/getCollegeAdmitByCollegeName")
    public Result getCollegeAdmitByCollegeName(@RequestBody JSONObject jsonObject){
        Integer currentPage = jsonObject.getInt("currentPage");
        String collegeName = jsonObject.getStr("collegeName");
        String provinceAdmit = jsonObject.getStr("provinceAdmit");
        QueryWrapper<CollegeAdmit> collegeAdmitQueryWrapper = new QueryWrapper<CollegeAdmit>().eq("college_name",collegeName);
        Page page = new Page(currentPage,10);
        if (!"全部".equals(provinceAdmit)){
            collegeAdmitQueryWrapper = collegeAdmitQueryWrapper.eq("province",provinceAdmit);
        }
        IPage<CollegeAdmit> iPage = collegeAdmitService.page(page,collegeAdmitQueryWrapper.orderByDesc("province","year"));

        if (iPage.getRecords().isEmpty()){
            return Result.success(201,"查询列表为空",iPage);
        }else{
            return Result.success(iPage);
        }
    }

    /**
     * 根据collegeName 预测各类型下年的录取分数
     * 返回list
     * @return Result
     */
    @PostMapping("/predictionNextYear")
    public Result predictionNextYear(@RequestBody JSONObject jsonObject){
        String collegeName = jsonObject.getStr("collegeName");
        String province = jsonObject.getStr("province");
        List<CollegeAdmit> collegeAdmitList = collegeAdmitService.list(new QueryWrapper<CollegeAdmit>().eq("college_name", collegeName).eq("province", province));
        if (collegeAdmitList.isEmpty()){
            return Result.success(201,"预测列表为空",null);
        }else{
            int scienceNum = 0;
            int liberalNum = 0;
            double scienceScoreSum = 0.0;
            double liberalScoreSum = 0.0;
            int scienceRankeSum = 0;
            int liberalRankSum = 0;
            for (CollegeAdmit collegeAdmit : collegeAdmitList) {
                if ("理科".equals(collegeAdmit.getExamType())){
                    //理科
                    scienceNum++;
                    scienceScoreSum += Double.parseDouble(collegeAdmit.getLowestScore());
                    scienceRankeSum += collegeAdmit.getLowestRank();

                }
                if ("文科".equals(collegeAdmit.getExamType())){
                    //文科
                    liberalNum++;
                    liberalScoreSum += Double.parseDouble(collegeAdmit.getLowestScore());
                    liberalRankSum += collegeAdmit.getLowestRank();
                }

            }
            ArrayList<PredictionNextYear> predictionNextYears = new ArrayList<>();
            if (scienceNum == 0 && liberalNum == 0 ){
                return Result.success(201,"预测列表为空",null);
            }else {
                if (scienceNum != 0){
                    Double scienceScore = Double.parseDouble(new DecimalFormat("0.00").format(scienceScoreSum / scienceNum));
                    PredictionNextYear sciencePredictionNextYear = new PredictionNextYear(province,2021,"理科",scienceScore,scienceRankeSum/scienceNum);
                    predictionNextYears.add(sciencePredictionNextYear);
                }
                if (liberalNum != 0){
                    Double liberalScore = Double.parseDouble(new DecimalFormat("0.00").format(liberalScoreSum/liberalNum));
                    PredictionNextYear liberalPredictionNextYear = new PredictionNextYear(province,2021,"文科",liberalScore,liberalRankSum/liberalNum);
                    predictionNextYears.add(liberalPredictionNextYear);
                }
                return Result.success(predictionNextYears);
            }
        }
    }

    /**
     * 根据collegeName province统计各类型的录取
     * 返回list
     * @return Result
     */
    @PostMapping("/countCollegeAdmit")
    public Result countCollegeAdmit(@RequestBody JSONObject jsonObject){
        String collegeName = jsonObject.getStr("collegeName");
        String province = jsonObject.getStr("province");
        List<CollegeAdmit> collegeAdmitList = collegeAdmitService.list(new QueryWrapper<CollegeAdmit>().eq("college_name", collegeName).eq("province", province).orderByAsc("year"));
        if (collegeAdmitList.isEmpty()){
            return Result.success(201,"统计列表为空",null);
        }else{
            return Result.success(collegeAdmitList);
        }
    }



}
